<template>
  <div class="ContentArticle">
    <el-card>
      <!-- 头部区域 -->
      <header-search>
        <template #search>
          <span>标题</span>
          <el-input
            v-model="queryData.title"
            placeholder="请输入标题名称"
            style="width: 220px"
            size="small"
          />
          <span>分类</span>
          <el-select v-model="queryData.cateid" placeholder="请选择" size="small" @focus="getArticleCate">
            <el-option
              v-for="item in articleList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            />
          </el-select>
          <span>时间</span>
          <el-date-picker
            v-model="time"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="small"
          />
          <el-button
            icon="el-icon-search"
            type="primary"
            size="small"
            @click="searchList"
          >搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="btnReset">重置</el-button>
        </template>

        <template #addContent>
          <el-button
            icon="el-icon-plus"
            type="primary"
            size="small"
            @click="$router.push('/content_article/add')"
          >新增</el-button>
          <!-- 导出组件 -->
          <export-upload type="article" />
        </template>
      </header-search>

      <!-- 表格 -->
      <el-table :data="tableData" border style="width: 100%" class="articleList">
        <el-table-column fixed prop="id" label="ID" />
        <el-table-column prop="title" label="标题" />
        <el-table-column prop="catename" label="分类名" />
        <el-table-column prop="pic" label="图片">
          <template #default="{ row }">
            <img :src="`http://124.223.14.236:8060/${row.pic ? row.pic : '' }`" alt="" class="articleImg">
          </template>
        </el-table-column>
        <el-table-column prop="create_date" label="创建时间">
          <template #default="{ row }">
            {{ handleTime(row.create_date) }}
          </template>
        </el-table-column>
        <el-table-column prop="ishot" label="是否推荐">
          <el-switch
            v-model="ishot"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </el-table-column>
        <el-table-column prop="istop" label="是否置顶">
          <el-switch
            v-model="istop"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </el-table-column>
        <el-table-column prop="status" label="文章状态">
          <template #default="{ row }">
            <div class="status">
              {{ handleStatus(row.status) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template #default="{ row }">
            <el-button icon="el-icon-edit" type="primary" size="small" @click="$router.push(`/content_article/edit/${row.id}`)" />
            <el-button icon="el-icon-delete" type="danger" size="small" @click="delArticle(row.id)" />
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination
        :current-page="queryData.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryData.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>
<script>
import { getArticleCateAllApi } from '@/api/contnet/cate'
import { getArticleListApi, delArticleApi } from '@/api/contnet/article'
import dayjs from 'dayjs'
export default {
  name: 'ContentArticle',
  data() {
    return {
      // 表格渲染数据
      tableData: [],
      // 下拉
      value: '',
      // 时间筛选
      time: '',
      // 请求参数
      queryData: {
        start_time: '',
        end_time: '',
        title: '',
        cateid: '',
        page: 1,
        limit: 10
      },
      total: null,
      // 是否推荐
      ishot: false,
      // 是否置顶
      istop: false,
      // 头部搜索区域分类
      articleList: []
    }
  },
  computed: {
  },
  created() {
    this.initData()
  },
  methods: {
    // 列表初始化
    async initData() {
      const { data: res } = await getArticleListApi(this.queryData)
      this.tableData = res.data
      // 处理是否推荐
      this.ishot = res.data.ishot === 1 ? this.ishot === true : this.ishot === false
      // 处理是否置顶
      this.istop = res.data.istop === 1 ? this.istop === true : this.istop === false
      this.total = res.count
    },
    // 分页器 => 每页显示条数
    handleSizeChange(val) {
      this.queryData.limit = val
      this.queryData.page = 1
      this.initData()
    },
    // 分页器 => 当前页
    handleCurrentChange(val) {
      this.queryData.page = val
      this.initData()
    },
    // 处理表格数据中的时间
    handleTime(time) {
      return dayjs(time).format('YYYY-MM-DD')
    },
    // 处理发布状态
    handleStatus(status) {
      if (status === 0) {
        return '待审核'
      }
      if (status === 1) {
        return '草稿箱'
      }
      if (status === 2) {
        return '已发布'
      }
      if (status === 3) {
        return '已删除'
      }
    },
    // 删除文章
    async delArticle(id) {
      try {
        await this.$confirm('确定删除吗？', '提示')
      } catch (error) {
        return console.log(error)
      }
      if (this.tableData.length === 1 && this.queryData.page > 1) {
        this.queryData.page--
      }
      await delArticleApi(id)
      this.$message.success('删除成功')
      this.initData()
    },
    // 搜索区域
    async searchList() {
      this.queryData.start_time = Date.now(this.queryData.start_time)
      this.queryData.end_time = Date.now(this.queryData.end_time)
      const res = await getArticleListApi(this.queryData)
      this.tableData = res.data.data
      this.total = res.data.count
    },
    // 重置
    btnReset() {
      this.queryData = {
        start_time: '',
        end_time: '',
        title: '',
        cateid: '',
        page: 1,
        limit: 10
      }
      this.initData()
    },
    // 获取文章分类
    async getArticleCate() {
      const { data: res } = await getArticleCateAllApi()
      this.articleList = res
    }
  }
}
</script>

<style lang="scss" scoped>
.ContentArticle {
  padding: 20px 20px 0 20px;
  .articleImg {
    height: 120px;
  }
  .el-pagination {
    margin-top: 20px;
    float: right;
  }
  .el-select {
    margin-right: 30px;
  }
  ::v-deep .el-date-editor {
    margin-right: 30px;
    width: 220px;
    margin-left: 10px;
  }
  .articleList {
    .el-button {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      padding: 0 ;
    }
  }
  .status {
    background-color: #ecf5ff;
    border-color: #d9ecff;
    color: #409EFF;
    width:60px;
    height: 32px;
    padding: 0 10px;
    line-height: 30px;
    font-size: 12px;
    border-width: 1px;
    border-style: solid;
    border-radius: 4px;
    box-sizing: border-box;
    white-space: nowrap;
  }
}
</style>
